package com.example.project.service.impl;

import com.example.project.dao.LotteryExtDao;
import com.example.project.model.bo.LotteryBo;
import com.example.project.model.bo.LotteryQueryDataVo;
import com.example.project.model.bo.PagingParam;
import com.example.project.service.LotteryExtBusinessService;
import com.example.project.service.base.impl.BaseBusinessServiceImpl;
import com.example.project.util.AssertUtils;
import com.example.project.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2017/12/10.
 */
@Service
public class LotteryExtBusinessServiceImpl extends BaseBusinessServiceImpl implements LotteryExtBusinessService {

    @Autowired
    LotteryExtDao lotteryExtDao;

    @Override
    public LotteryQueryDataVo loadLotteryList(PagingParam pagingParam) {
        LotteryQueryDataVo lotteryQueryDataVo = new LotteryQueryDataVo();
        List<LotteryBo> lotteryBoList =   lotteryExtDao.loadLotteryList(pagingParam);
        if(AssertUtils.isNotEmpty(lotteryBoList)){
            lotteryBoList.stream().map(lotteryBo -> {
                lotteryBo.setShowTime(DateUtils.timeStrToString(lotteryBo.getLotteryTime(),"MM-dd HH:mm:ss"));
                return lotteryBo;
            }).collect(Collectors.toList());
        }
        lotteryQueryDataVo.setTotalNumber(lotteryExtDao.loadLotteryListCount());
        lotteryQueryDataVo.setPrepage(pagingParam.getPrepage());
        lotteryQueryDataVo.setPage(pagingParam.getPage());
        lotteryQueryDataVo.setLotterys(lotteryBoList);

        //查询最新一期
       LotteryBo lotteryBo = lotteryExtDao.loadNewestLotteryPo();
       if(AssertUtils.isNotNull(lotteryBo)){
           lotteryQueryDataVo.setFirstText(lotteryBo.getFirstText());
           lotteryQueryDataVo.setSecondText(lotteryBo.getSecondText());
           lotteryQueryDataVo.setThirdText(lotteryBo.getThirdText());
           lotteryQueryDataVo.setFirstUrl(lotteryBo.getFirstUrl());
           lotteryQueryDataVo.setSecondUrl(lotteryBo.getSecondUrl());
           lotteryQueryDataVo.setThirdUrl(lotteryBo.getThirdUrl());
           lotteryQueryDataVo.setIssueNo(lotteryBo.getIssueNo());
       }
        return lotteryQueryDataVo;
    }

    @Override
    public LotteryQueryDataVo loadLotteryChatList(PagingParam pagingParam) {
        LotteryQueryDataVo lotteryQueryDataVo = new LotteryQueryDataVo();
        List<LotteryBo> lotteryBoList =   lotteryExtDao.loadLotteryList(pagingParam);
        if(AssertUtils.isNotEmpty(lotteryBoList)){
            lotteryBoList.stream().map(lotteryBo -> {
                if(lotteryBo.getFirstNum()>lotteryBo.getSecondNum() && lotteryBo.getFirstNum()>lotteryBo.getThirdNum()){
                    if(lotteryBo.getSecondNum()>lotteryBo.getThirdNum()){
                        lotteryBo.setBayValue(lotteryBo.getFirstNum()-lotteryBo.getThirdNum());
                    }else{
                        lotteryBo.setBayValue(lotteryBo.getFirstNum()-lotteryBo.getSecondNum());
                    }
                }else if(lotteryBo.getSecondNum()>lotteryBo.getThirdNum() && lotteryBo.getSecondNum()>lotteryBo.getFirstNum()){
                    if(lotteryBo.getFirstNum()>lotteryBo.getThirdNum()){
                        lotteryBo.setBayValue(lotteryBo.getSecondNum()-lotteryBo.getThirdNum());
                    }else{
                        lotteryBo.setBayValue(lotteryBo.getSecondNum()-lotteryBo.getFirstNum());
                    }
                }else if(lotteryBo.getThirdNum()>lotteryBo.getSecondNum() && lotteryBo.getThirdNum()>lotteryBo.getFirstNum()){
                    if(lotteryBo.getFirstNum()>lotteryBo.getSecondNum()){
                        lotteryBo.setBayValue(lotteryBo.getThirdNum()-lotteryBo.getSecondNum());
                    }else{
                        lotteryBo.setBayValue(lotteryBo.getThirdNum()-lotteryBo.getFirstNum());
                    }
                }
                //单双
                if(lotteryBo.getSumNum()%2 == 1){
                    lotteryBo.setSingle("单");
                    lotteryBo.setSingleNum(1);
                }else{
                    lotteryBo.setSingle("双");
                    lotteryBo.setSingleNum(2);
                }

                return lotteryBo;
            }).collect(Collectors.toList());
        }

        lotteryQueryDataVo.setTotalNumber(lotteryExtDao.loadLotteryListCount());
        lotteryQueryDataVo.setPrepage(pagingParam.getPrepage());
        lotteryQueryDataVo.setPage(pagingParam.getPage());
        lotteryQueryDataVo.setLotterys(lotteryBoList);

        return lotteryQueryDataVo;
    }

    /**
     * 查询下拉框的数据
     * @return
     */
    @Override
    public LotteryQueryDataVo loadLotteryDownList() {
        List<LotteryBo> lotteryBoList = lotteryExtDao.loadLotteryDownList();
        LotteryQueryDataVo lotteryQueryDataVo=new LotteryQueryDataVo();
        lotteryQueryDataVo.setLotterys(lotteryBoList);
        long a=0;
        if(AssertUtils.isNotEmpty(lotteryBoList)){
            //当前2点钟执行完了
            if(DateUtils.timeStrToString(lotteryBoList.get(0).getLotteryTime(),"HH:mm:ss").equals("02:00:00")){
                a=28800-(System.currentTimeMillis()-lotteryBoList.get(0).getLotteryTime())/1000;
            }else {
                //设置最新的倒计时时间
                a = 300-(System.currentTimeMillis()-lotteryBoList.get(0).getLotteryTime())/1000;
            }
        }else {
            a=300;
        }

        System.err.println(a);
        lotteryQueryDataVo.setCountdown(a);

        return lotteryQueryDataVo;
    }

    @Override
    public LotteryBo loadLotteryBo(long issueNo) {

        return lotteryExtDao.loadLotteryBo(issueNo);
    }

    @Override
    public LotteryQueryDataVo loadInputLotteryList(PagingParam pagingParam) {
        LotteryQueryDataVo lotteryQueryDataVo = new LotteryQueryDataVo();
        List<LotteryBo> lotteryBoList =   lotteryExtDao.loadInputLotteryList(pagingParam);
        int totalLine =0;
        if(AssertUtils.isNotEmpty(lotteryBoList)){
            totalLine =lotteryBoList.get(0).getTotalLine();
            lotteryBoList.stream().map(lotteryBo -> {
                lotteryBo.setShowTime(DateUtils.timeStrToString(lotteryBo.getLotteryTime(),"MM-dd HH:mm:ss"));
                return lotteryBo;
            }).collect(Collectors.toList());
        }
        lotteryQueryDataVo.setTotalNumber(totalLine);
        lotteryQueryDataVo.setPrepage(pagingParam.getPrepage());
        lotteryQueryDataVo.setPage(pagingParam.getPage());
        lotteryQueryDataVo.setLotterys(lotteryBoList);

        //查询最新一期
        LotteryBo lotteryBo = lotteryExtDao.loadNewestLotteryPo();
        if(AssertUtils.isNotNull(lotteryBo)){
            lotteryQueryDataVo.setFirstText(lotteryBo.getFirstText());
            lotteryQueryDataVo.setSecondText(lotteryBo.getSecondText());
            lotteryQueryDataVo.setThirdText(lotteryBo.getThirdText());
            lotteryQueryDataVo.setFirstUrl(lotteryBo.getFirstUrl());
            lotteryQueryDataVo.setSecondUrl(lotteryBo.getSecondUrl());
            lotteryQueryDataVo.setThirdUrl(lotteryBo.getThirdUrl());
            lotteryQueryDataVo.setIssueNo(lotteryBo.getIssueNo());
        }
        return lotteryQueryDataVo;
    }
}
